package CMessage
{
	import Classes.pi_events;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class cm_userInterface extends Object {
		protected static const DESCRIPTION: String = 'Что бы посмотреть, как будет выглядеть открытка с текстом, нажмите кнопку «Полный размер» на панели инструментов.'; 
		
		private var _app	: Object;
		private var _classes: Object;
		private var _button	: Object;
		private var _dialog : Object;
//		private var _name	: Object;
		private var _text	: Object;
		private var _extendsLayer: Object;
		
		
		public function cm_userInterface(a_app: Object, a_classes: Object, extendsLayer: Object) {
			_app = a_app;
			_app.styles['eui_button'] = _app.merge('tabBlock', {
				component: {
					buttonMode: true,
					mouseChildren: false
				}
			});
			super();
			_extendsLayer = extendsLayer;
			_classes = a_classes;
			_app.addEventListener(pi_events.PHOTOLOADCOMPLETE, doPhotoLoad);
			togleUserInterface();
		}
		
		protected function doPhotoLoad(e: Object): void {
			togleUserInterface();
		}
		
		public function get editMode(): Boolean {
			return !app.isFullView && (app.editor != null); 
		}
		
		public function togleUserInterface(e: Object = null): void {
/*			if (app.isPhotoLoaded && editMode) {
				if (!_button) createUserInterface();
				_button.slideShow();
			} else if (_button) {
				_button.slideHide();
			}*/
		}
		
		protected function doShowDialog(e: Object = null): void {
			if (!_dialog) createDialog();
			var params: Array = (app.cardbody.maskInfo.extend && app.cardbody.maskInfo.extend['MSG'])?app.cardbody.maskInfo.extend['MSG']:[];
			_text.text = '';
			if (params.length > 0) {
//				_name.text = params[1].replace('0x002C', ',');
				_text.text = cm_message.decode(params[1]);
			}
			_dialog.show();
		}
		
		protected function createInput(lText: String, iValue: String, ly: Number, lh: Number=20): Object {
			(new _classes['label'](_dialog, 20, ly, 60, 20, 'box_label')).text = lText; 
			var input: Object = new _classes['label'](_dialog, 85, ly, 300, lh, 'minput');
			input.text = iValue; 
			return input;
		}
		
		protected function createDialog(): void {
			var size: Point = new Point(400, 300);
			var rect: Object = app.EnterHere(size);
			_dialog = new _classes['window'](app.layers[4], rect.x, rect.y, rect.width, rect.height, 'window', true, 1);
			_dialog.title = 'Текст к открытке';
			
			var inner: Rectangle = _dialog.getInner();
			(new _classes['label'](_dialog, inner.x, inner.y, inner.width, 60, 'description')).text = DESCRIPTION;
//			_name = createInput('От кого:', _classes['c_app'].userName(app.userData), 100);*/
			_text = createInput('Текст:', '', 80, 170);
			var okButton: Object = new _classes['button'](_dialog, 200 - 82, 260, 80, 30, 'button');
			var cancelButton: Object = new _classes['button'](_dialog, 200 + 2, 260, 80, 30, 'button');
			okButton.text = 'Ок';
			cancelButton.text = 'Отмена';
			okButton.addEventListener(MouseEvent.CLICK, doApply);
			cancelButton.addEventListener(MouseEvent.CLICK, doClose);
		}
		
		protected function doApply(e: Object): void {
			if (!app.cardbody.maskInfo.extend) app.cardbody.maskInfo.extend = new Object();
			
			if (_text.text) app.cardbody.maskInfo.extend['MSG'] = ['MSG', cm_message.encode(_text.text)];
			else delete(app.cardbody.maskInfo.extend['MSG']);															
			doClose(e);
		}
		
		protected function doClose(e: Object): void {
			_dialog.close(1);
		}
		
		protected function createUserInterface(): void {
			_button = new _classes['button'](_extendsLayer, 0, 105, 250, 30, 'eui_button');
			_button.text = 'Добавить/изменить текст к открытке';
			_button.addEventListener(MouseEvent.CLICK, doShowDialog);
		}
		
		public function get app(): Object {
			return _app;
		}
	}
}